const express = require('express')
const router = express.Router()
const jwt = require('jsonwebtoken')

const userList = []
const loginhandler = (req, res) => {
    const userinfo = req.body
    // console.log(userinfo);
    
    // 用户名不能为空的判断
    if(!userinfo.username){
        return res.send({
            status:1,
            message:'用户名不能为空'
        })
    }
    // 查看用户名是否重复
    if(userList.find(item => item.username === userinfo.username)){
        return res.send({
            status:1,
            message:'用户名被占用，请更换用户名'
        })
    }else{
        userList.push(userinfo)
        const token = jwt.sign({...userinfo},'login',{expiresIn:'10h'})
        return res.send({
            status:0,
            message:'登录成功',
            token:'Bearer '+token,
            userList
        })
    }
}

router.post('/login',loginhandler)

module.exports = {router,userList}